/**
 * @file CustomToast.java
 * @create 下午8:46:22
 * @author CX
 * @description 自定义的Toast
 */
package cn.edu.hstc.wm.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.hstc.wm.utils.LogUtil;

import cn.edu.hstc.wm.R;

public class CustomToast {
	public Context mContext;
	public ImageView imageView;
	public TextView textView;
	public Toast mToast;
	public View rootView;
	public static CustomToast mCustomToast;

	private CustomToast(Context context) {
		if (mToast == null) {
			rootView = LayoutInflater.from(context).inflate(R.layout.custom_toast_layout, null);
			initView();
			mToast = new Toast(context);
			// mToast.setView(rootView);
			// defaultPro();
		} else {
			LogUtil.v("CustomToast", "CustomToast not null ");
		}

	}

	/**
	 * @param context
	 *            上下文
	 * @param text
	 *            显示内容
	 * @param time
	 *            1:较长时间显示 ; 0:较短时间显示
	 */
	public static void show(Context context, String text, int time) {
		Toast.makeText(context, text, time == 1 ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT).show();
	}

	public static CustomToast getInstance(Context context) {
		CustomToast toast = new CustomToast(context);
		return toast;
	}

	private void initView() {
		imageView = (ImageView) rootView.findViewById(R.id.ivForToast);
		textView = (TextView) rootView.findViewById(R.id.tvForToast);
	}

	// 默认设置
	@SuppressWarnings("unused")
	private void defaultPro() {

		mToast.setDuration(Toast.LENGTH_LONG);
		mToast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0);
		mToast.setMargin(0, 0);
	}

	/**
	 * 使用自定义toast时调用该静态方法后再调用设置参数的方法,最后在调用show()来显示
	 * 
	 * @param context
	 * @return
	 */
	public static CustomToast make(Context context) {
		mCustomToast = new CustomToast(context);
		return mCustomToast;
	}

	/**
	 * 调用其他方法设置后调用这个来显示
	 * 
	 * @param context
	 */
	public void show() {
		mToast.setView(rootView);
		mToast.show();
	}

	/**
	 * 设置指定的View
	 * 
	 * @param view
	 * @return
	 */
	public CustomToast setView(View view) {
		mToast.setView(rootView);
		return mCustomToast;
	}

	/**
	 * 设置toast显示位置
	 * 
	 * @param gravity
	 *            对齐位置
	 * @param xOffset
	 *            x偏移
	 * @param yOffset
	 *            y偏移
	 * @return
	 */
	public CustomToast setGravity(int gravity, int xOffset, int yOffset) {
		mToast.setGravity(gravity, xOffset, yOffset);

		return mCustomToast;
	}

	/**
	 * 设置外边距 使用长宽的%来表示
	 * 
	 * @param horizontalMargin
	 *            水平外边距到toast显示的%距离
	 * @param verticalMargin
	 * @return
	 */
	public CustomToast setMargin(float horizontalMargin, float verticalMargin) {
		mToast.setMargin(horizontalMargin, verticalMargin);
		return mCustomToast;
	}

	/**
	 * 设置自定义toast显示时间长短
	 * 
	 * @param duration
	 *            0 显示短,1显示长
	 * @return
	 */
	public CustomToast setDuration(int duration) {
		switch (duration) {
		case 0:
			mToast.setDuration(Toast.LENGTH_SHORT);
			break;
		case 1:
			mToast.setDuration(Toast.LENGTH_LONG);
			break;
		default:
			mToast.setDuration(Toast.LENGTH_LONG);
			break;
		}

		return mCustomToast;
	}

	/** 设置自定义toast提示文字 */
	public CustomToast setText(String text) {
		// mToast.setText(text);
		textView.setText(text);
		textView.setTextSize(16);
		setDuration(1);
		return mCustomToast;
	}

	/**
	 * 设置字体大小
	 * 
	 * @param size
	 * @return
	 */
	public CustomToast setTextSize(float size) {
		textView.setTextSize(size);
		return mCustomToast;
	}

	/** 设置自定义toast提示文字 */
	public CustomToast setText(int textRes) {
		return setText(mContext.getResources().getString(textRes));
	}

	/** 设置自定义toast背景 */
	public CustomToast setBackground(int resid) {
		imageView.setBackgroundResource(resid);
		imageView.setVisibility(View.VISIBLE);
		return mCustomToast;
	}

	/** 设置自定义toast背景 */
	@SuppressLint("NewApi")
	public CustomToast setBackground(Drawable drawable) {
		imageView.setBackground(drawable);
		imageView.setVisibility(View.VISIBLE);
		return mCustomToast;
	}

	/**
	 * 取消该toast显示
	 */
	public void cancel() {
		mToast.cancel();
	}
}
